package paytreasure.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;

import paytreasure.dao.BillDao;
import paytreasure.po.Accountdetail;
import paytreasure.service.BillService;
import paytreasure.util.ConstantUtil;
import paytreasure.vo.ResilrAccountdetail;
import paytreasure.vo.ResultVo;

public class BillServiceImpl implements BillService {
	
	@Autowired
	private BillDao billDao;
	
	private ResultVo result;
	
	public ResultVo saveBill(Accountdetail bill) {
		// TODO Auto-generated method stub
		int i = billDao.insert(bill);
		if (i == 0) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "数据保存异常！");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "保存成功！");
		return result;
	}
	
	public ResultVo updateBill(Accountdetail bill) {
		int i = billDao.update(bill);
		if (i == 0) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "数据修改异常！");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "修改成功！");
		return result;
	}
	
	//查询总条数
	public int getCount(HashMap<String, String> hashMap) {
		return billDao.getCount(hashMap);
	}
	
	//查询总条数
	public ResultVo getCondition(HashMap<String, String> hashMap) {
		List<ResilrAccountdetail> list = billDao.getCondition(hashMap);
		
		if(list.size() == 0) {
			result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "没有更多数据");
			return result;
		}
		
		//按日期分组
		//Map<String, List<ResilrAccountdetail>> map = new HashMap<String, List<ResilrAccountdetail>>();
//		Map<String, List<ResilrAccountdetail>> map = new TreeMap<String, List<ResilrAccountdetail>>(new Comparator<String>() {
//			/* 
//             * int compare(Object o1, Object o2) 返回一个基本类型的整型， 
//             * 返回负数表示：o1 小于o2， 
//             * 返回0 表示：o1和o2相等， 
//             * 返回正数表示：o1大于o2。 
//             */ 
//			public int compare(String o1, String o2) {
//				// TODO Auto-generated method stub
//				return o2.compareTo(o1);
//			}
//		});
		
		TreeMap<String, List<ResilrAccountdetail>>  map = new TreeMap<String, List<ResilrAccountdetail>>();//Map<String, List<ResilrAccountdetail>>();
		List<ResilrAccountdetail> newList = new ArrayList<ResilrAccountdetail>();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for (ResilrAccountdetail account : list) {
			String creatDate = "";
			String creatHourM = "";
			if(null != account.getCreateDate()){
				creatDate = formatter.format(account.getCreateDate());
				creatHourM = formatter1.format(account.getCreateDate());
				creatHourM = creatHourM.substring(11,16);
			//	System.err.println(creatHourM);
			}else {
				creatDate = "暂无日期";
			}
			
			if(map.containsKey(creatDate)) {
				map.get(creatDate).add(account);
			}else {
				account.setCreateYearM(creatDate);
				account.setCreateHourM(creatHourM);
				newList.add(account);
			}
		}
		
		NavigableMap<String,List<ResilrAccountdetail>> map1 = map.descendingMap();
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "获取成功！",newList);
		//System.out.println(result);
		return result;
	}

	//查询总条数
	public ResultVo getById(String billId) {
		Accountdetail bill = billDao.get(billId);
		if(bill == null) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "未获取到数据");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "查询成功！",bill);
		return result;
	}
}
